/**
\ * Copyright &copy; 2012-2014 <a href="http://www.zhiqone.com">http://www.zhiqone.com</a> All rights reserved.
 */
package com.zcm.modules.center.web.center;

import com.zcm.common.config.Global;
import com.zcm.common.utils.StringUtils;
import com.zcm.common.web.BaseController;
import com.zcm.modules.center.entity.CenterUser;
import com.zcm.modules.center.service.CenterUserService;
import com.zcm.modules.cms.utils.CmsUtils;
import com.zcm.modules.sys.service.SystemService;
import com.zcm.common.web.BaseController;
import com.zcm.modules.center.entity.CenterUser;
import com.zcm.modules.center.service.CenterUserService;
import com.zcm.modules.sys.service.SystemService;
import com.zcm.modules.zhaocai.entity.ZcmToolVersion;
import com.zcm.modules.zhaocai.service.ZcmToolVersionService;
import org.activiti.engine.impl.util.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户中心Controller
 * @author www
 * @version 2018-04-22
 */
@Controller
@RequestMapping(value = "${centerPath}")
public class CenterController extends BaseController {
	@Autowired
	private CenterUserService centerUserService;

	@Autowired
	private ZcmToolVersionService versionService;

    //获取软件版本和下载链接
    @RequestMapping(value = "latestVersion")
    @ResponseBody
    public Map<String, Object> latestVersion() throws JSONException {
        ZcmToolVersion query = new ZcmToolVersion();
        Map<String, Object> map=new HashMap<String, Object>();
        query.setEnabled("1");
        List<ZcmToolVersion> list = versionService.findList(query);
        if(list.size() == 0){
            map.put("success",false);
            map.put("code",1);
            map.put("message","无可用版本");
            return map;
        }
		ZcmToolVersion v = list.get(0);
		String baseDir = Global.getUserfilesBaseDir();
		URI uri = new File(baseDir).toURI();
		try {
			String strUri = uri.toString() + v.getFilePath().replaceFirst("^/.+?/","");
			uri = new URI( strUri.replaceAll("//",""));
		} catch (URISyntaxException e) {
			e.printStackTrace();
		}
		File f = new File(uri);
		if(f.exists() && f.isFile()){
			v.setFileSize(f.length());
		}

        map.put("success",true);
        map.put("message","");
        map.put("code",0);
        map.put("version",v);
        return map;
    }
	//登陆
	@RequestMapping(value = "login",method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> login(HttpServletRequest request) throws JSONException {
		String loginName = request.getParameter("loginName");
		String passWord = request.getParameter("passWord");
		String clientVersion = request.getParameter("clientVersion");
		Map<String, Object> map=new HashMap<String, Object>();
		//HttpSession session = request.getSession();
		CenterUser centerUser = null;
		if(StringUtils.isNumeric(loginName)){
			centerUser = centerUserService.getUserByMobile(loginName);
			if(centerUser==null){
				centerUser = centerUserService.getUserByLoginName(loginName);
			}
		}else{
			centerUser = centerUserService.getUserByLoginName(loginName);
		}
		if(null==centerUser) {
			map.put("success", false);
			map.put("code", 1);
			map.put("message", "用户不存在");
			return map;
		}

        // 校验密码
        if(!SystemService.validatePassword(passWord,centerUser.getPassword())){
            map.put("success", false);
			map.put("code", 2);
            map.put("message", "密码不正确");
            return map;
		}

		centerUser.setUpdateDate(new Date());
		centerUser.setLoginDate(new Date());
		centerUser.setLoginIp(StringUtils.getRemoteAddr(request));
		centerUserService.save(centerUser);

		ZcmToolVersion versionQuery = new ZcmToolVersion();
		versionQuery.setVersion(clientVersion);
		List<ZcmToolVersion> list = versionService.findList(versionQuery);
		if(list.size()==0){
			map.put("success", false);
			map.put("code", 3);
			map.put("message", "版本不可用");
			return map;
		}
		ZcmToolVersion version = list.get(0);
		if("0".equals(version.getEnabled())){
			map.put("success", false);
			map.put("code", 3);
			map.put("message", "该版本不可用");
			return map;
		}

		map.put("user", centerUser);
		map.put("success", true);
		map.put("code", 0);
		map.put("message", "登陆成功");
		return map;
	}
}